package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.PartBaseInfo;
import com.sl.au.entity.Hierarchy;
import com.sl.au.entity.Option;
import com.sl.au.entity.Part;
import com.sl.au.service.HierarchyService;
import com.sl.au.service.IdentifyService;
import com.sl.au.service.OptionService;
import com.sl.au.service.PartBaseInfoService;
import com.sl.utils.MsgHelper;

/**
 * 部件绑定保养部门
 * 
 * @author yangdi
 *
 */
@RestController
@RequestMapping(value = "/ui/maintainHierarchy")
public class PartMaintainHierarchyController extends BaseController {

	@Autowired
	HierarchyService hierarchyService; 
	
	@Autowired
	PartBaseInfoService partBaseInfoService;
	
	@Autowired
	OptionService optionService;
	
	@Autowired
	IdentifyService identifyService;
	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView modelAndView = this.createLayoutView("partBaseInfo/maintainHierarchy");
		return modelAndView;
	}
	
	/**
	 * 获取部件区域树信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		List<Hierarchy> node = null;
		
		node = hierarchyService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=hierarchyService.getNodeMap(node);
		
		map.put("code", 1);
		map.put("nodes", nodeMap);
	
		return map;
	}
	
	/**
	 * 获取未绑定维护部门的信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String,Object> getPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<PartBaseInfo> modules=null;
		int count = 0;
		
		modules = partBaseInfoService.getPageByNoMaintainHierarchy(start, length, key, "createTime", Sort.Direction.DESC);
		count = partBaseInfoService.getCountByNoMaintainHierarchy(key);
		
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 获取已绑定维护部门的信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpagebyhierarchy", method = RequestMethod.POST)
	public Map<String,Object> getPageByPart(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String hierarchyId=request.getParameter("hierarchyId");
		List<PartBaseInfo> modules=null;
		int count = 0;
		modules = partBaseInfoService.getPageByMaintainHierarchy(start, length, hierarchyId, key, "createTime", Sort.Direction.DESC);
		count = partBaseInfoService.getCountByMaintainHierarchy(hierarchyId, key);
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	
	/**
	 * 绑定维护部门关系
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/binding",method = RequestMethod.POST)
	public MsgHelper bindingHierarchy(@RequestBody Map<String,Object> map){
		MsgHelper msgHelper = MsgHelper.success();
		String id = (String) map.get("id");
		String hierarchyId = String.valueOf(map.get("hierarchyId"));
		
		PartBaseInfo partBaseInfo = partBaseInfoService.getRepository().findOne(id);
		Hierarchy hierarchy = hierarchyService.getRepository().findOne(hierarchyId);
		partBaseInfo.setMaintainHierarchy(hierarchy);
		partBaseInfoService.getRepository().save(partBaseInfo);
		msgHelper.setMsg("部件维护部门绑定成功!");
		
		return msgHelper;
	};
	
	/**
	 * 解除绑定维护部门
	 * @param id
	 * @return
	 */
	@RequestMapping("/unbind/{id}")
	public MsgHelper unbindHierarchy(@PathVariable("id") String id) {
		MsgHelper msgHelper = MsgHelper.success();
		PartBaseInfo partBaseInfo = partBaseInfoService.getRepository().findOne(id);
		
		partBaseInfo.setMaintainHierarchy(null);;
		
		partBaseInfoService.getRepository().save(partBaseInfo);
		
		msgHelper.setMsg("部件维护部门移除成功!");
		
		return msgHelper;
	}
	
	
	@RequestMapping(value="/addDataAll",method = RequestMethod.POST)
	public Map<String,Object> addDataAll(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		String Id = (String) map.get("Id");
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		Hierarchy hierarchy = hierarchyService.getRepository().findOne(Id);
		for(String partBaseInfoId : dataInfo) {
				PartBaseInfo partBaseInfo = partBaseInfoService.getRepository().findOne(partBaseInfoId);
				partBaseInfo.setMaintainHierarchy(hierarchy);
				partBaseInfoService.getRepository().save(partBaseInfo);
		}
		msg.put("msg", "维护部门绑定成功!");
		return msg;
	};
	
	@RequestMapping(value="/removeDataAll",method = RequestMethod.POST)
	public Map<String,Object> removeDataAll(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		for(String partBaseInfoId : dataInfo) {
			PartBaseInfo partBaseInfo = partBaseInfoService.getRepository().findOne(partBaseInfoId);
			partBaseInfo.setMaintainHierarchy(null);
			partBaseInfoService.getRepository().save(partBaseInfo);
		}
		msg.put("msg", "维护部门移除成功!");
		return msg;
	};
	
}
